/**
 * Created by jun on 2017/6/29.
 * 事件侦听，触发管理
 */
var logManager=require("./Logs.js");
module.exports=Class.extend({
    _listenerMap:null,
    _preventedEvent:null,
    _preCurrentEventDispatch:false,
    ctor:function(){
        this._listenerMap={};
        this._preventedEvent=[];
    },
    /**
     * 添加事件侦听
     * @param event     事件名称
     * @param callback  回调方法
     * @param thisObj   this引用
     */
    init:function(){
        logManager.logM("event manager init")
    },
    /**
     * 取消当前事件的默认行为(默认行为由事件的触发者处理)
     */
    preventDefault:function(){
        if(this._preventedEvent&&this._preventedEvent.length) {
            // logManager.logM("preventDefault:");
            this._preventedEvent[this._preventedEvent.length - 1] = true;
        }
    },
    preCurrentEventDispatch:function(){
        this._preCurrentEventDispatch=true;
    },
    /**
     * add a event listener
     * @param event
     * @param callback
     * @param thisObj
     * @param level the biggest one will call at first
     */
    addListener:function(event, callback,thisObj,level){
        if(!callback || !event) {
            if(!callback)
                logManager.logW("add event lisener error:"+event+"callback is null");
            else
                logManager.logW("add event lisener error:event is null");
            return;
        }
        if(!this._listenerMap[event])
            this._listenerMap[event]=[];
        var listenerList=this._listenerMap[event];
        for (var i = 0; i < listenerList.length; i++) {
            if(event==listenerList[i].event&&listenerList[i].callBack == callback&&listenerList[i].thisObj==thisObj){
                logManager.logW("Redefine event lisener---------"+event);
                return;
            }
        }
        if(!level)
            level=0;
        listenerList.push({event:event,callBack:callback,thisObj:thisObj,level:level});
        listenerList.sort(function(a,b){
            return b.level-a.level
        })
    },
    /**
     * 添加事件侦听
     * @param event     事件名称
     * @param callback  回调方法
     * @param thisObj   this引用
     */
    removeListener:function(event, callback,thisObj){
        if(!callback || !event)
            return;
        var listenerList = this._listenerMap[event];
        if(listenerList)
        {
            for (var i = 0; i < listenerList.length; i++)
            {
                if(listenerList[i].callBack === callback&&listenerList[i].thisObj===thisObj)
                {
                    listenerList.splice(i, 1);
                    i--;
                }
            }
        }
    },
    /**
     * 触发事件
     * @param event
     * @param data
     */
    dispatchEvent:function(event,data){
        this._preventedEvent.push(false);
        data=data.slice();
        data.push(event);
        if(this._listenerMap[event])
        {
            var listeners = this._listenerMap[event].slice();
            for (var i = 0; i < listeners.length; i++) {
                var fun=listeners[i].callBack;
                var thisObj=listeners[i].thisObj;
                fun.apply(thisObj,data);
                if(this._preCurrentEventDispatch){
                    this._preCurrentEventDispatch=false;
                    break;
                }
            }
        }
        return this._preventedEvent.pop();
    }
});